/**
 * Copyright &copy; 2015-2020 <a href="http://www.easyhis.com/">easyhis</a> All rights reserved.
 */
package com.easy.modules.sys.service.impl;

import com.easy.common.util.Utility;
import com.easy.core.persistence.Page;
import com.easy.core.service.impl.BaseServiceImpl;
import com.easy.modules.sys.entity.Office;
import com.easy.modules.sys.mapper.OfficeMapper;
import com.easy.modules.sys.service.OfficeService;
import com.easy.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 机构Service
 * @author easy
 * @version 2017-05-16
 */
@Service
@Transactional(readOnly = true)
public class OfficeServiceImpl  extends BaseServiceImpl implements OfficeService {

	@Autowired
	private OfficeMapper mapper;
	
	public List<Office> findAll(){
		return UserUtils.getOfficeList();
	}

	public List<Office> findList(Boolean isAll){
		if (isAll != null && isAll){
			return UserUtils.getOfficeAllList();
		}else{
			return UserUtils.getOfficeList();
		}
	}

	
	@Transactional(readOnly = true)
	public Office getByCode(String code){
		return mapper.getByCode(code);
	}
	
	public List<Office> getChildren(String id){
		return mapper.getChildren(id);
	}

	/**
	 * 获取单条数据
	 * @param id
	 * @return
	 */
	public Office get(String id) {
		return mapper.get(id);
	}

	/**
	 * 获取单条数据
	 * @param officeId
	 * @return
	 */
	public Office getByOfficeId(Integer officeId) {
		return mapper.getByOfficeId(officeId);
	}

	/**
	 * 获取单条数据
	 * @param office
	 * @return
	 */
	public Office get(Office office) {
		return mapper.get(office);
	}

	/**
	 * 查询列表数据
	 * @param office
	 * @return
	 */
	public List<Office> findList(Office office) {
		dataRuleFilter(office);
		return mapper.findList(office);
	}

	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param office
	 * @return
	 */
	public Page<Office> findPage(Page<Office> page, Office office) {
		dataRuleFilter(office);
		office.setPage(page);
		page.setList(mapper.findList(office));
		return page;
	}

	/**
	 * 保存数据（插入或更新）
	 * @param office
	 */
	@Transactional(readOnly = false)
	public void save(Office office) {
		if (Utility.isNullorEmpty(office.getId()) || office.getOfficeId()<1){
			office.preInsert();
			mapper.insert(office);
		}else{
			office.preUpdate();
			mapper.update(office);
		}
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}

	/**
	 * 删除数据
	 * @param office
	 */
	@Transactional(readOnly = false)
	public void delete(Office office) {
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
		mapper.delete(office);
	}


	/**
	 * 删除全部数据
	 * @param offices
	 */
	@Transactional(readOnly = false)
	public void deleteAll(Collection<Office> offices) {
		for (Office office : offices) {
			mapper.delete(office);
		}
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}

	/**
	 * 删除全部数据
	 * @param offices
	 */
	@Transactional(readOnly = false)
	public void deleteAllByLogic(Collection<Office> offices) {
		for (Office office : offices) {
			mapper.deleteLogic(office);
		}
		UserUtils.removeCache(UserUtils.CACHE_OFFICE_LIST);
	}


	/**
	 * 获取单条数据
	 * @param propertyName, value
	 * @return
	 */
	public Office findUniqueByProperty(String propertyName, Object value){
		return mapper.findUniqueByProperty(propertyName, value);
	}

	/**
	 * 动态sql
	 * @param sql
	 */

	public List<Object> executeSelectSql(String sql){
		return mapper.execSelectSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeInsertSql(String sql){
		mapper.execInsertSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeUpdateSql(String sql){
		mapper.execUpdateSql(sql);
	}

	@Transactional(readOnly = false)
	public void executeDeleteSql(String sql){
		mapper.execDeleteSql(sql);
	}



}
